package com.bsj.power.pls.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.excel.EasyExcelFactory;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.def.dto.path.PathSearchDTO;
import com.bsj.power.common.def.vo.pls.FormsPathVO;
import com.bsj.power.common.def.vo.pls.PathReportCountVO;
import com.bsj.power.common.def.vo.pls.PathReportVO;
import com.bsj.power.common.def.vo.pls.excel.AlarmReportCountExcelVO;
import com.bsj.power.common.def.vo.pls.excel.PathReportCountExcelVO;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.pls.service.PathReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description PathReportController
 * @time 2024/5/7 11:47
 */
@RestController
@RequestMapping("/pathReport")
//@SaCheckPermission("/pls/pathReport")
public class PathReportController {

    @Autowired
    private PathReportService pathReportService;

    /**
     * 区域进出统计报表
     *
     * @param pathSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.pls.PathReportCountVO>>
     * @author ljx
     * @time 2024/5/7 14:45
     */
    @GetMapping("/page")
    public JsonResult<List<PathReportCountVO>> page(@Validated PathSearchDTO pathSearchDTO) {
        return pathReportService.pagePathReportCount(pathSearchDTO);
    }

    /**
     * 区域进出详情
     *
     * @param pathSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.pls.PathReportVO>>
     * @author ljx
     * @time 2024/5/7 19:20
     */
    @GetMapping("/detail")
    public JsonResult<List<PathReportVO>> pathReportDetail(@Validated PathSearchDTO pathSearchDTO) {
        return pathReportService.pathReportDetail(pathSearchDTO);
    }

    /**
     * 获取区域进出报表
     *
     * @param pathSearchDTO
     * @return
     */
    @GetMapping("/pagePathReport")
    public JsonResult<List<PathReportCountVO>> pagePathReport(@Validated PathSearchDTO pathSearchDTO) {
        return pathReportService.pagePathReport(pathSearchDTO);
    }

    /**
     * 进出区域统计图表
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @SaIgnore
    @GetMapping("/formsReport")
    public JsonResult<List<FormsPathVO>> formsReport(String startTime, String endTime) {
        return pathReportService.formsReport(startTime, endTime);
    }

    /**
     * 导出进出围栏数据
     *
     * @param response
     * @param pathSearchDTO
     * @throws IOException
     */
    @GetMapping(value = "/exportPathReport")
    public void exportPathReport(HttpServletResponse response, PathSearchDTO pathSearchDTO) throws IOException {
        pathSearchDTO.setPageNumber(1);
        pathSearchDTO.setPageSize(Integer.MAX_VALUE);
        JsonResult<List<PathReportCountVO>> jsonResult = pathReportService.pagePathReport(pathSearchDTO);
        List<PathReportCountVO> data = jsonResult.getData();
        if (CollectionUtils.isEmpty(data)) {
            throw new ApiException(JsonResultEnum.NO_EXPORT_DATA);
        }
        List<PathReportCountExcelVO> list = new ArrayList<>();
        data.stream().forEach(item -> {
            PathReportCountExcelVO excelVO = new PathReportCountExcelVO();
            excelVO.add(item);
            list.add(excelVO);
        });
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("进出区域记录" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), PathReportCountExcelVO.class).sheet("进出区域记录")
                .doWrite(list);
    }
}
